<script setup>
import { ref } from 'vue';
import router from '../router';

const addressId = ref(null)
const address = ref(null)
const name = ref(null)
const phone = ref(null)
//标题导航栏
const onClickLeft = () => history.back();
const onClickRight = () => {
  router.push({
    path: '/createOrder',
    query: {
      addressId: addressId.value,
      address: address.value,
      name: name.value,
      phone: phone.value
    }
  })
}
import { useRoute } from 'vue-router';
const route = useRoute();
const rightTex = ref('确定')
const rightDisabled = ref(false)
if(route.query.isUser){
  rightTex.value = ''
  rightDisabled.value = true
}

import { addressBookList } from '../apis/addressBook.js'
const list = ref([])
async function getAddressBookList() {
  const res = await addressBookList()
  list.value = res.data.data
  if(list.value.length==0||list.value==null){
    rightTex.value = ''
    rightDisabled.value = true
  }
  list.value.forEach(item => {
    if (item.isDefault == 1) {
      addressId.value = item.id
      address.value = item.address
      name.value = item.name
      phone.value = item.tel
      chosenAddressId.value = item.id
    }
  })
}
getAddressBookList()
const chosenAddressId = ref();
/*  const list = [
   {
     id: '1',
     name: '张三先生',
     tel: '13000000000',
     address: '浙江省杭州市西湖区文三路 138 号东方通信大厦 7 楼 501 室',
     isDefault: true,
   },
   {
     id: '2',
     name: '李四',
     tel: '1310000000',
     address: '浙江省杭州市拱墅区莫干山路 50 号',
   },
 ]; */
const onAdd = () => {
  router.push('/editorAddAdress')
}
const onEdit = (item, index) => {
  router.push({
    path: '/editorAddAdress',
    query: {
      id:item.id,
      consignee:item.name.split(' ')[0],
      sex:(item.name.split(' ')[1]==='先生'?1:0),
      phone:item.tel,
      building:item.address.split(' ')[0],
      doorplate:item.address.split(' ')[1],
      isDefault:item.isDefault
    }
  })
  console.log(item, index);
}



const select = (item, index) => {
  console.log(item, index);
  addressId.value = item.id
  address.value = item.address
  name.value = item.name
  phone.value = item.tel

}
</script>
<template>
  <div>
    <van-nav-bar title="地址管理" left-text="返回" :right-text="rightTex" :right-disabled="rightDisabled" left-arrow
      @click-left="onClickLeft" @click-right="onClickRight" />
  </div>
  <van-address-list v-model="chosenAddressId" :list="list" default-tag-text="默认" @select="select" @add="onAdd"
    @edit="onEdit" />


</template>